package com.carkeeper.shop.controller;

import com.carkeeper.entity.PageResult;
import com.carkeeper.entity.Result;
import com.carkeeper.entity.StatusCode;
import com.carkeeper.shop.pojo.ServiceType;
import com.carkeeper.shop.service.TypeItemService;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/typeItem")
@Api(tags = "子分类接口")
public class TypeItemController {
    @Autowired
    private TypeItemService typeItemService;

    /**
     * 查询父类分类名称
     * @param id
     * @return
     */
    @GetMapping("/findParentName/{id}")
    @ApiOperation("查询父分类名称接口")
    private Result findNameById(@PathVariable("id") String id){
        String typeName = typeItemService.findTypeNameById(id);
        return new Result(true, StatusCode.OK,"",typeName);
    }


    /**
     * 条件分页查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/findPage/{page}/{size}")
    @ApiOperation("条件分页查询接口")
    private Result findPage(@RequestParam Map<String,Object> searchMap,@PathVariable("page") int page,@PathVariable("size") int size){
        if (searchMap!=null&&searchMap.size()>0) {
            Page<ServiceType> typeItemPage = typeItemService.findPage(searchMap, page, size);
            PageResult<ServiceType> pageResult = new PageResult<>(typeItemPage.getTotal(), typeItemPage.getResult());
            return new Result(true,StatusCode.OK,"查询成功",pageResult);
        }
        return new Result(false,StatusCode.ERROR,"没有查询条件！");
    }


    /**
     * 分页查询
     * @return
     */
    @GetMapping("/findItemPage/{page}/{size}")
    @ApiOperation("分页查询接口")
    private Result findPage(@RequestParam("id") String id,@PathVariable("page") int page,@PathVariable("size") int size){
        Page<ServiceType> pageList = typeItemService.findPage(id,page, size);
        PageResult<ServiceType> pageResult = new PageResult<>(pageList.getTotal(), pageList.getResult());
        return new Result(true,StatusCode.OK,"",pageResult);
    }

    /**
     * 添加
     * @param serviceType
     * @param id
     * @return
     */
    @PostMapping("/add/{id}")
    @ApiOperation("添加子分类接口")//标注方法
    private Result addTypeItem(@RequestBody ServiceType serviceType,@PathVariable("id") String id){
        typeItemService.addTypeItem(serviceType,id);
        return new Result(true,StatusCode.OK,"新增成功");
    }

    /**
     * 修改
     * @param serviceType
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("修改子分类接口")//标注方法
    private Result updateTypeItem(@RequestBody ServiceType serviceType){
        typeItemService.updateTypeItem(serviceType);
        return new Result(true, StatusCode.OK,"修改成功");
    }

    /**
     * 删除服务分类
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除子分类接口")
    private Result deleteTypeById(@PathVariable("id") String id){
        try {
            typeItemService.deleteTypeItem(id);
            return new Result(true,StatusCode.OK,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"无法删除");
        }
    }
}
